<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\jooj_member;

class MemberVip extends Base {

    protected $base_table = 'jj_member_vip';
    protected $table = 'jj_member_vip';
    protected $connection = ['database' => 'jooj_member'];

    public function __construct($company_id = []) {
        if (!is_array($company_id)) {
            $this->table = $this->base_table . '_' . $company_id;
        }

        parent::__construct($company_id);
    }

    /**
     * 添加memberVip数据
     * @param type $data
     */
    public function addMemberVip($data) {

        $insertData = [
            'member_code' => isset($data['member_code']) ? $data['member_code'] : null,
            'cardno' => isset($data['cardno']) ? $data['cardno'] : null,
            'card_lev' => isset($data['card_lev']) ? $data['card_lev'] : null,
            'mobile' => isset($data['mobile']) ? $data['mobile'] :null,
            'name' => isset($data['name']) ? $data['name'] : null,
            'gender' => isset($data['gender']) ? intval($data['gender']) : null,
            'birthday' => isset($data['birthday']) ? $data['birthday'] : null,
            'first_p' => isset($data['first_p']) ? $data['first_p'] : null,
            'first_store' => isset($data['first_store']) ? $data['first_store'] : null,
            'first_qrscene' => isset($data['first_qrscene']) ? $data['first_qrscene'] :null,
            'province' => $data['province'],
            'zipcode' => $data['zipcode'],
            'city' => $data['city'],
            'area' => $data['area'],
            'add_ts' => time(),
            'last_ts' => time(),
        ];
        
        $insertData = array_filter($insertData);
        return $this->insertGetId($insertData);
    }

    /**
     * 通过vipId获取view信息
     * @param type $vipId
     * @param type $field 因为vip信息不能全部查询出来，只能走view，所以针对vip信息只能查部分
     * @return type
     */
    public function getMemberVipByVipId($vipId, $field = "province,city,area,adress,zipcode,integral,integral_use,remark,add_ts,last_ts") {
        return $this->setTable($this->table)->where(['vip_id' => $vipId])->field($field)->find();
    }

    /**
     * 根据vipId去更新单字段
     * (用于MemberService->updateMemberField尽量不要复用此方法，没办法写的low方法)
     * @param type $field
     * @param type $value
     * @param type $vipId
     */
    public function updateValue($field, $value, $vipId) {
        if (empty($value)) {
            return true;
        }
        //将数据更新进去
        $this->setTable($this->table)->where(['vip_id' => $vipId])->update(["{$field}" => $value]);
        return true;
    }

    /**
     * 更新vip表数据，通过vipId
     * @param type $params
     * @param type $vipId
     * @return type
     */
    public function updateMemberVipByVipId($vipId, $params = []) {

        //同步更新为了获取和memberIndex的数据同步保持一直
        $data = [
            'name' => isset($params['name']) ? $params['name'] : null,
            'gender' => isset($params['gender']) ? $params['gender'] : null,
            'birthday' => isset($params['birthday']) ? $params['birthday'] : null,
            'province' => isset($params['province']) ? $params['province'] :null,
            'city' => isset($params['city']) ? $params['city'] : null,
            'area' => isset($params['area']) ? $params['area'] : null,
            'adress' => isset($params['address']) ? $params['address'] : null,
            'zipcode' => isset($params['zipcode']) ? $params['zipcode'] :null,
        ];
        //过滤掉空的数据
        $data = array_filter($data);

        $res = $this->where(['vip_id' => $vipId])->update($data);

        if ($res === false) {
            return false;
        }
        return true;
    }

    /**
     * 通过条件获取Vip表的数据
     * @param type $condition
     * @param type $field
     * @return type
     */
    public function getMemberVipByCondition($condition, $field = '*') {

        return $this->setTable($this->table)->where($condition)->field($field)->find();
    }

    /**
     * 注册会员
     * @param array $params
     * @param boolean $status
     * @return type
     */
    public function registerVipMember($params, $companyId, $status = false) {
        $userData = $this->addUser($params);
        $params['member_code'] = $userData[0]['member_code'];
        //写入账号服务信息表
        $this->addIndexInfo($params);

        //写入到正式会员表
        $vipId = $this->addVIp($params);
        $viewMemberModel = new ViewMemberIndex();
        $data = $viewMemberModel->getIndexAndvipData(['member_code' => $params['member_code']]);
        $status = true;
        $message = '正式会员';
        $signData = [
            'memberCode' => $params['member_code'],
            'vipId' => $vipId,
            'serviceStore' => $params['first_store'],
            'comeFrom' => 4,
            'serviceP' => $params['first_p'],
        ];
        $this->sign($signData);
        return ['status' => $status, 'data' => ['code' => '0', 'desc' => '恭喜您,注册' . $message . '成功', 'data' => $data]];
    }

    /**
     * 分页获取vip表的列表
     * @param type $page
     * @param type $pageSize
     * @param type $where
     * @return type
     */
    public function getPage($page = 1, $pageSize = 2, $where = [], $field = "*") {


        $page = $page < 1 ? 1 : intval($page);
        $pageSize = $pageSize > 100 ? 100 : ($pageSize < 2 ? 2 : intval($pageSize));

        //修正分页参数
        $count = $this->getPageCount($where);
        $pageList = $this->getPageCount($where, 0, $page, $pageSize, $field);

        $data = [
            'totalCount' => $count,
            'totalPage' => ceil($count / $pageSize),
            'list' => $pageList,
        ];

        return $data;
    }

    /**
     * 获取分页数据和count
     * @param type $where
     * @param type $is_count
     * @param type $page
     * @param type $pageSize
     * @param type $field
     * @return type
     */
    public function getPageCount($where, $is_count = 1, $page = 1, $pageSize = 2, $field = "*") {

        $page = $this;

        if ($where) {
            $page = $page->where($where);
        }

        if ($is_count) {
            return $page->count();
        }

        return $page->page($page, $pageSize)->field($field)->select();
    }

    /**
     * 
     * @param type $params
     * @return type
     */
    public function addVip($params) {
        $data['member_code'] = $params['member_code'];
        $data['name'] = $params['name'];
        $data['gender'] = $params['gender'];
        $data['birthday'] = $params['birthday'];
        $data['mobile'] = $params['mobile'];
        //$data['mobile_status'] = $params['mobileStatus'];
        //$data['come_from'] = $params['comeFrom'];
        $data['add_ts'] = time();
        $data['last_ts'] = time();
        $data['province'] = $params['province'];
        $data['city'] = $params['city'];
        $data['area'] = $params['area'];
        $data['cardno'] = $params['cardno'];
        $data['adress'] = $params['adress'];
        $data['zipcode'] = $params['zipcode'];
        $data['first_p'] = $params['firstP'];
        $data['first_store'] = $params['firstStore'];
        return $this->insertGetId($data);
    }

    /**
     * 获取会员信息
     * @param type $condition
     * @param type $feild
     * @return type
     */
    public function getMemberVipData($condition, $feild) {
        return $this->field($feild)->where($condition)->find();
    }

    /**
     * 更新会员表
     * @param type $condition
     * @param array $data
     * @return type
     */
    public function updateVipData($condition, $data) {
        unset($data['mobile_status']);
        $data['last_ts'] = time();
        return $this->where($condition)->update($data);
    }

    /**
     * 更新vip信息
     * @param type $condtion
     * @param type $params
     * @return type
     */
    public function updateMemberVip($condtion, $params) {
        $iData = [
            'name'=>isset($params['name'])?$params['name']:null,
            'gender'=>isset($params['gender'])?$params['gender']:null,
            'cardno'=>isset($params['cardno'])?$params['cardno']:null,
            'card_lev'=>isset($params['card_lev'])?$params['card_lev']:null,
            'birthday'=>isset($params['birthday'])?$params['birthday']:null,
            'province'=>isset($params['province'])?$params['province']:null,
            'city'=>isset($params['city'])?$params['city']:null,
            'area'=>isset($params['area'])?$params['area']:null,
            'adress'=>isset($params['address'])?$params['address']:null,
            'zipcode'=>isset($params['zipcode'])?$params['zipcode']:null,
            'last_ts'=>time()
        ];
        return $this->where($condtion)->update(array_filter($iData));
    }

}
